﻿<template>
  <a-modal
    title="编辑会员消费"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="IC_NO " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入IC_NO " v-decorator="['icNo']" />
        </a-form-item>
        <a-form-item label="PATIENT_NAME " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入PATIENT_NAME " v-decorator="['patientName']" />
        </a-form-item>
        <a-form-item label="OPER_DATE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择OPER_DATE " v-decorator="['operDate']" @change="onChangeoperDate"/>
        </a-form-item>
        <a-form-item label="OPER_CODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入OPER_CODE " v-decorator="['operCode']" />
        </a-form-item>
        <a-form-item label="OPER_NAME " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入OPER_NAME " v-decorator="['operName']" />
        </a-form-item>
        <a-form-item label="OUT_FEE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入OUT_FEE " v-decorator="['outFee']" />
        </a-form-item>
        <a-form-item label="OPER_STYLE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入OPER_STYLE " v-decorator="['operStyle']" />
        </a-form-item>
        <a-form-item label="REMARK " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REMARK " v-decorator="['remark']" />
        </a-form-item>
        <a-form-item label="OPER_FROM " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入OPER_FROM " v-decorator="['operFrom']" />
        </a-form-item>
        <a-form-item label="RCPT_NO " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入RCPT_NO " v-decorator="['rcptNo']" />
        </a-form-item>
        <a-form-item label="REBATE_TYPE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入REBATE_TYPE " v-decorator="['rebateType']" />
        </a-form-item>
        <a-form-item label="IC_TYPE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入IC_TYPE " v-decorator="['icType']" />
        </a-form-item>
        <a-form-item label="IC_UNION_CODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入IC_UNION_CODE " v-decorator="['icUnionCode']" />
        </a-form-item>
        <a-form-item label="APP_OPER_DATE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择APP_OPER_DATE " v-decorator="['appOperDate']" @change="onChangeappOperDate"/>
        </a-form-item>
        <a-form-item label="APP_ORDER_CODE " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入APP_ORDER_CODE " v-decorator="['appOrderCode']" />
        </a-form-item>
        <a-form-item label="PATIENT_NAME_UNION " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入PATIENT_NAME_UNION " v-decorator="['patientNameUnion']" />
        </a-form-item>
        <a-form-item label="CRM_ID " :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入CRM_ID " v-decorator="['crmId']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['iD']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import {
    IcOutListEdit
  } from '@/api/modular/main/IcOutListManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        operDateDateString: '',
        appOperDateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              iD: record.iD,
              icNo: record.icNo,
              patientName: record.patientName,
              operCode: record.operCode,
              operName: record.operName,
              outFee: record.outFee,
              operStyle: record.operStyle,
              remark: record.remark,
              operFrom: record.operFrom,
              rcptNo: record.rcptNo,
              rebateType: record.rebateType,
              icType: record.icType,
              icUnionCode: record.icUnionCode,
              appOrderCode: record.appOrderCode,
              patientNameUnion: record.patientNameUnion,
              crmId: record.crmId
            }
          )
        }, 100)
        this.form.getFieldDecorator('operDate', { initialValue: moment(record.operDate, 'YYYY-MM-DD') })
        this.operDateDateString = moment(record.operDate).format('YYYY-MM-DD')
        this.form.getFieldDecorator('appOperDate', { initialValue: moment(record.appOperDate, 'YYYY-MM-DD') })
        this.appOperDateDateString = moment(record.appOperDate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.operDate = this.operDateDateString
            values.appOperDate = this.appOperDateDateString
            IcOutListEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangeoperDate(date, dateString) {
        this.operDateDateString = dateString
      },
      onChangeappOperDate(date, dateString) {
        this.appOperDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
